package com.jianyun.jyzs.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmSynchroActivity;
import com.jianyun.jyzs.bean.CrmListResponse;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by mlli on 2017/8/16.
 */

public class CrmSynAdapter extends RecyclerView.Adapter<CrmSynAdapter.MyHolder> {
    private CrmSynchroActivity context;
    public List<CrmListResponse.CrmListBean> list;
    private List<CrmListResponse.CrmListBean> selectedIds = new ArrayList<>();//选中的条码id
    private boolean checkAll = false;



    public CrmSynAdapter(CrmSynchroActivity context, List<CrmListResponse.CrmListBean> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public CrmSynAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_crm_synchro, null);
        return new MyHolder(view);
    }

    public void setCheckAll(){
        this.checkAll = !checkAll;
        context.tvChecked.setText("同步");
        selectedIds.clear();
        if (checkAll){
            selectedIds.addAll(this.list);
            context.tvChecked.setText("同步("+selectedIds.size()+")");
        }
        notifyDataSetChanged();
    }
    public List<CrmListResponse.CrmListBean> getSelectedList(){
        return selectedIds;
    }

    public List<CrmListResponse.CrmListBean> getList() {
        return list;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final CrmListResponse.CrmListBean bean = list.get(position);
        //UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(bean.getCreateUserId());
//        if (userInfo!=null){
//            Picasso.with(context).load(userInfo.getPortraitUri()).into(holder.head);
//        }else {
            Picasso.with(context).load(R.drawable.rce_ic_option_group_chat).into(holder.head);
        //}
        holder.headName.setText(bean.getCreateUserName());
        holder.tvTitle.setText(bean.getName());
        holder.tvState.setText("状态:"+bean.getFunnel());
        holder.tvProfile.setText(bean.getCreateDate());
        holder.tvPhoneCount.setText(bean.getPhoneTimes()+"");
        holder.tvTalkCount.setText(bean.getVisitTimes()+"");

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    if (!selectedIds.contains(bean))selectedIds.add(bean);
                    context.tvChecked.setText("同步("+selectedIds.size()+")");
                }else {
                    selectedIds.remove(bean);
                    if (selectedIds.size()>0){
                        context.tvChecked.setText("同步("+selectedIds.size()+"");
                    }else {
                        context.tvChecked.setText("同步");
                    }
                }
            }
        });

        if (checkAll){
            holder.checkbox.setChecked(true);
        }else {
            holder.checkbox.setChecked(false);
        }

    }

    public void setList(List<CrmListResponse.CrmListBean> list) {
        this.list = list;
        checkAll = false;
        selectedIds.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }


    class MyHolder extends RecyclerView.ViewHolder {
        ImageView head;
        TextView headName;
        LinearLayout headParent;
        TextView tvTitle;
        TextView hasLocal;
        CheckBox checkbox;
        TextView tvState;
        TextView tvCity;
        TextView tvProfile;
        TextView tvPhoneCount;
        TextView tvTalkCount;
        RelativeLayout synchroRelative;


        public MyHolder(View itemView) {
            super(itemView);
            head = (ImageView) itemView.findViewById(R.id.head);
            headName = (TextView) itemView.findViewById(R.id.headName);
            headParent = (LinearLayout) itemView.findViewById(R.id.headParent);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            hasLocal = (TextView) itemView.findViewById(R.id.hasLocal);
            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            tvState = (TextView) itemView.findViewById(R.id.tv_state);
            tvCity = (TextView) itemView.findViewById(R.id.tv_city);
            tvProfile = (TextView) itemView.findViewById(R.id.tv_profile);
            tvPhoneCount = (TextView) itemView.findViewById(R.id.phoneCount);
            tvTalkCount = (TextView) itemView.findViewById(R.id.tv_talkCount);
            synchroRelative = (RelativeLayout) itemView.findViewById(R.id.synchroRelative);
        }

    }
}
